#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>		/* usleep */


#include "bluetooth.h"
#include "public.h"

#define SEND_AT_INSTRUCTION(instruction)	uart_send((unsigned char *)instruction, strlen(instruction))


/**
 * bluetooth_mqtt_send_receive - bluetooth at instructions send and receive.
 * @at_instruction			: at instruction
 * @instruction_exec_status : comparison value
 */
static unsigned char bluetooth_send_receive(const char * at_instruction, const char * instruction_exec_status)
{
	unsigned char receive_buffer[BT_BUFFER_LEN] = { 0 };

	__exhibit(at_instruction);

	/* at instruction send */
	SEND_AT_INSTRUCTION(at_instruction);

	/* receive instruction return value */
	uart_receive(receive_buffer, BT_BUFFER_LEN);
	printf("%d:%s\n", strlen((char*)receive_buffer), receive_buffer);

	/* find status */
	if (strstr((char *)receive_buffer, instruction_exec_status) != NULL) {
		return 0;
	}
	else {
		return 0xf;
	}
}

/**
 * bluetooth_at_mode - entry AT mode.
 * @none
 */
unsigned char bluetooth_at_mode(void)
{
	if (bluetooth_send_receive((char *) BT_ENTER_AT, (char *) AT_INSTRUCTION_OK ) == 0) {
		ac_traces("info : AT mode config success.\n");
	}
	else {
		ac_traces("[[ error: AT mode version config fail...]]\r\n");
		return 0xff;
	}

	return 0;
}

/**
 * bluetooth_chip_version - get chip version.
 * @none
 */
unsigned char bluetooth_chip_version(void)
{
	if (bluetooth_send_receive((char *) BT_VER, (char *) AT_INSTRUCTION_OK ) == 0) {
		ac_traces("info : chip version read success.\n");
	}
	else {
		ac_traces("[[ error: chip version read fail...]]\r\n");
		return 0xff;
	}

	return 0;
}

/**
 * bluetooth_set_uart - set uart.
 * @none
 */
unsigned char bluetooth_set_uart(void)
{
	char at_instruction[BT_INSTRUCTION_LEN] = { 0 };

	sprintf(at_instruction, BT_SET_UART, 115200, 8, 1, 0, 50);

	if (bluetooth_send_receive((char *) at_instruction, (char *) AT_INSTRUCTION_OK ) == 0) {
		ac_traces("info : set uart success.\n");
	}
	else {
		ac_traces("[[ error: set uart fail...]]\r\n");
		return 0xff;
	}

	return 0;
}

/**
 * bluetooth_get_serinum - get serial number.
 * @none
 */
unsigned char bluetooth_get_serinum(void)
{
	if (bluetooth_send_receive((char *) BT_GET_SERINUM, (char *) AT_INSTRUCTION_OK ) == 0) {
		ac_traces("info : get serial number success.\n");
	}
	else {
		ac_traces("[[ error: get serial number fail...]]\r\n");
		return 0xff;
	}

	return 0;
}

/**
 * bluetooth_get_sysid - get system id.
 * @none
 */
unsigned char bluetooth_get_sysid(void)
{
	if (bluetooth_send_receive((char *) BT_SYSID, (char *) AT_INSTRUCTION_OK ) == 0) {
		ac_traces("info : get system id success.\n");
	}
	else {
		ac_traces("[[ error: get system id fail...]]\r\n");
		return 0xff;
	}

	return 0;
}

unsigned char bluetooth_get_mac(void)
{
	if (bluetooth_send_receive((char *) BT_GET_MAC, (char *) AT_INSTRUCTION_OK ) == 0) {
		ac_traces("info : get system id success.\n");
	}
	else {
		ac_traces("[[ error: get system id fail...]]\r\n");
		return 0xff;
	}

	return 0;
}

unsigned char bluetooth_set_mac(char aa, char bb)
{
	char at_instruction[BT_INSTRUCTION_LEN] = { 0 };

	sprintf(at_instruction, BT_SET_MAC, 0xac, 0x11, 0x22, 0x33, aa, bb);

	if (bluetooth_send_receive((char *) at_instruction, (char *) AT_INSTRUCTION_OK ) == 0) {
		ac_traces("info : set uart success.\n");
	}
	else {
		ac_traces("[[ error: set uart fail...]]\r\n");
		return 0xff;
	}

	return 0;
}

/**
 * BluetoothGetAdevnStatus - 获取广播使能控制状态(ON|OFF).
 * @none
 */
int BluetoothGetAdevnStatus(void)
{
	if (bluetooth_send_receive((char *) BT_GET_ADVEN, (char *) AT_INSTRUCTION_OK ) == 0) {
		ac_traces("info : get Broadcast enable control status success.\n");
	}
	else {
		ac_traces("[[ error: get Broadcast enable control status fail...]]\r\n");
		return -1;
	}

	return 0;
}

/**
 * BluetoothBroadcasEnable - 使能广播使能控制(ON).
 * @none
 */
int BluetoothBroadcasEnable(void)
{
	char pAtInstruction[BT_INSTRUCTION_LEN] = { 0 };

	sprintf(pAtInstruction, BT_SET_ADVEN, "ON");

	if (bluetooth_send_receive((char *) pAtInstruction, (char *) AT_INSTRUCTION_OK ) == 0) {
		ac_traces("info : set Broadcast Enable success.\n");
	}
	else {
		ac_traces("[[ error: set Broadcast Enable fail...]]\r\n");
		return -1;
	}

	return 0;
}

/**
 * BluetoothBroadcastData - 广播数据.
 * @none
 */
//int BluetoothBroadcastData(char * message)
int BluetoothBroadcastData()
{
	char pAtInstruction[BT_INSTRUCTION_LEN] = { 0 };

	sprintf(pAtInstruction, BT_SET_ADVDAT, "xag bluetooth message for test!!!!!!");

	if (bluetooth_send_receive((char *) pAtInstruction, (char *) AT_INSTRUCTION_OK ) == 0) {
		ac_traces("info : Broadcast Data success.\n");
	}
	else {
		ac_traces("[[ error: Broadcast Data fail...]]\r\n");
		return -1;
	}

	return 0;
}

/**
 * BluetoothGetBroadcastChannel - 获取广播通道.
 * @none
 */
int BluetoothGetBroadcastChannel(void)
{
	if (bluetooth_send_receive((char *) BT_GET_BCCH, (char *) AT_INSTRUCTION_OK ) == 0) {
		ac_traces("info : get Broadcast Channel success.\n");
	}
	else {
		ac_traces("[[ error: get Broadcast Channel fail...]]\r\n");
		return -1;
	}

	return 0;
}

/**
 * BluetoothSetBroadcastChannel - 设置广播通道.
 * @none
 */
int BluetoothSetBroadcastChannel(void)
{
	char pAtInstruction[BT_INSTRUCTION_LEN] = { 0 };

	sprintf(pAtInstruction, BT_SET_BCCH, 6);

	if (bluetooth_send_receive((char *) pAtInstruction, (char *) AT_INSTRUCTION_OK ) == 0) {
		ac_traces("info : set Broadcast Channel success.\n");
	}
	else {
		ac_traces("[[ error: set Broadcast Channel fail...]]\r\n");
		return -1;
	}
}

/**
 * BluetoothGetWorkMode - 获取蓝牙工作模式.
 * @none
 */
int BluetoothGetWorkMode(void)
{
	if (bluetooth_send_receive((char *) BT_GET_BLEMODE, (char *) AT_INSTRUCTION_OK ) == 0) {
		ac_traces("info : get workmode success.\n");
	}
	else {
		ac_traces("[[ error: get workmode fail...]]\r\n");
		return -1;
	}

	return 0;
}

/**
 * BluetoothSetWorkMode - 设置蓝牙工作模式.
 * @none
 */
int BluetoothSetWorkMode(int sMode)
{
	char pAtInstruction[BT_INSTRUCTION_LEN] = { 0 };

	sprintf(pAtInstruction, BT_SET_BLEMODE, sMode);
	//SEND_AT_INSTRUCTION(pAtInstruction);

	#if 1
	if (bluetooth_send_receive((char *) pAtInstruction, (char *) AT_INSTRUCTION_OK ) == 0) {
		ac_traces("info : set workmode success.\n");
	}
	else {
		ac_traces("[[ error: set workmode fail...]]\r\n");
		return -1;
	}
	#endif

	return 0;
}

/**
 * BluetoothChipReset - 蓝牙芯片重启.
 * @none
 */
int BluetoothChipReset(void)
{
	SEND_AT_INSTRUCTION((char *) BT_RESET);
	ac_traces("info : chip reset success.\n");

	return 0;
}

int BluetoothBroadcastMode(char mode)
{
	bluetooth_at_mode();
	usleep(200*1000);
	bluetooth_set_uart();
	usleep(200*1000);
	BluetoothGetWorkMode();
	usleep(200*1000);
	BluetoothSetWorkMode(mode);
	usleep(200*1000);
	BluetoothChipReset();
	usleep(1000*1000);
}
